package model.game.maze;

import java.util.ArrayList;

import model.algorithms.IDistance;
import model.algorithms.IAction;
import model.algorithms.IDomain;
import model.algorithms.State;

public class MazeDomain implements IDomain {
	private Maze maze;

	public MazeDomain(Maze maze) {
		this.maze = maze;
	}

//	public double g(State from, State to) {
//		MazeState mazeFrom = (MazeState)from;
//		MazeState mazeTo = (MazeState)to;
//		
//		// Moved diagonally
//		if (mazeFrom.getX() != mazeTo.getX() && mazeFrom.getY() != mazeTo.getY()) {
//			return 15;
//		}
//		// Moved straight
//		else {
//			return 10;
//		}
//	}
//
//	public double h(State state, State goal) {
//		MazeState mazeState = (MazeState)state;
//		MazeState mazeGoal = (MazeState)goal;
//		
//		int cols = Math.abs(mazeGoal.getX() - mazeState.getX());
//		int rows = Math.abs(mazeGoal.getY() - mazeState.getY());
//					
//		return (Math.sqrt(Math.pow(cols, 2) + Math.pow(rows, 2))) * 10;
//	}

	public ArrayList<IAction> getActions(State state) {
		MazeState mazeState = (MazeState)state;
		
		ArrayList<IAction> actions = new ArrayList<IAction>();

		for (int xIndex = -1; xIndex <= 1; xIndex++) {
			for (int yIndex = -1; yIndex <= 1; yIndex++) {
				if (xIndex != 0 || yIndex != 0) {
					int x = mazeState.getX() + xIndex;
					int y = mazeState.getY() + yIndex;
				
					int mazeValue = this.maze.get(x, y);
					
					if (mazeValue == 0 || mazeValue == 2) {
						actions.add(new MazeAction(xIndex, yIndex));
					}
				}
			}
		}

		return actions;
	}

	public double distance(State state, State goal) {
		IDistance g = new MazeG();
		return g.getDistance(state, goal);
	}

	public State getStartState() {
		return maze.getStartState();
	}

	public State getGoalState() {
		return maze.getGoalState();
	}
}
